package com.zz.cc.owner.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.zz.cc.owner.R;
import com.zz.cc.owner.TitleBaseActivity;
import com.zz.cc.owner.data.CarBrand;
import com.zz.common.widget.ZBladeView;
import com.zz.common.widget.ZPinnedHeaderListView;
import com.zz.common.widget.ZPinnedHeaderListView.PinnedHeaderAdapter;

public class RegisterCarBrandActivity extends TitleBaseActivity
		implements OnItemClickListener {
	private ZPinnedHeaderListView mListView;
	private ZBladeView mBladeView;
	
	private final ArrayList<BrandItem> mBrandItems = new ArrayList<BrandItem>();
	private final ArrayList<String> mSelections = new ArrayList<String>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_car_brand);
		initUI();
		fillView();
	}
	
	private void initUI() {
		mListView = (ZPinnedHeaderListView) findViewById(R.id.car_brank_list_view);
		mBladeView = (ZBladeView) findViewById(R.id.car_brank_blade_view);
	}
	
	private void fillView() {
		setCurrentTitle(R.string.card_brand_title);
		mListView.setOnItemClickListener(this);
	}
	
	private void loadData() {
		mBrandItems.clear();
		mSelections.clear();
		
		ArrayList<CarBrand> brands = null;
		//TODO load brands
		brands = new ArrayList<CarBrand>();
		if(null != brands) {
			//sort the brand list
			Collections.sort(brands, mCarBrandComparator);
			
			//init the indexs
			String firstChar = null;
			BrandItem curItem = null;
			int index = -1;
			for(CarBrand brand: brands) {
				if(firstChar != brand.mFirstPinyinChar) {
					firstChar = brand.mFirstPinyinChar;
					index++;
					BrandItem item = new BrandItem(firstChar, index);
					mBrandItems.add(item);
					curItem = item;
					mSelections.add(firstChar);
				} else {
					BrandItem item = new BrandItem(brand, index);
					mBrandItems.add(item);
					curItem.mCount++;
				}
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View convertView, int position, long itemId) {
		ViewHolder holder = (ViewHolder) convertView.getTag();
		if(null != holder && holder.mBrandItem.mViewType == CarBrandAdapter.TYPE_ITEM) {
			Intent intent = new Intent(this, RegisterCarModelActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
		}
	}
	
	private Comparator<CarBrand> mCarBrandComparator = new Comparator<CarBrand>() {

		@Override
		public int compare(CarBrand lhs, CarBrand rhs) {
			int r = 0;
			do {
				r = lhs.mFirstPinyinChar.compareTo(rhs.mFirstPinyinChar);
				if(r != 0) {
					break;
				}
				r = lhs.mBrandPinyin.compareTo(rhs.mBrandPinyin);
				if(r != 0) {
					break;
				}
				r = lhs.mBrand.compareTo(rhs.mBrand);
			} while(false);
			return r;
		}
		
	};
	
	private static class BrandItem {
		int mViewType;
		int mIndex;
		
		CarBrand mCarBrand;
		
		String mHeader;
		int mCount;
		
		
		public BrandItem(String header, int index) {
			mViewType = CarBrandAdapter.TYPE_HEADER;
			mHeader = header;
			mCount = 0;
			mIndex = index;
		}
		
		public BrandItem(CarBrand carBrand, int index) {
			mViewType = CarBrandAdapter.TYPE_ITEM;
			mCarBrand = carBrand;
			mCount = 0;
			mIndex = index;
		}
	}
	
	private static class ViewHolder {
		TextView mTvTitle;
		BrandItem mBrandItem;
	}
	
	private class CarBrandAdapter extends BaseAdapter implements SectionIndexer
					, PinnedHeaderAdapter, OnScrollListener {

		public static final int TYPE_ITEM = 0;
		public static final int TYPE_HEADER = TYPE_ITEM + 1;
		public static final int TYPE_COUNT = TYPE_HEADER;
		

		@Override	//BaseAdapter
		public int getCount() {
			return mBrandItems.size();
		}

		@Override	//BaseAdapter
		public BrandItem getItem(int position) {
			return mBrandItems.get(position);
		}

		@Override	//BaseAdapter
		public long getItemId(int position) {
			return 0;
		}

		@Override	//BaseAdapter
		public View getView(int position, View convertView, ViewGroup parent) {
			BrandItem item = getItem(position);
			if(item.mViewType == TYPE_ITEM) {
				return getItemView(item, convertView);
			} else {
				return getHeaderView(item, convertView);
			}
		}
		
		private View getHeaderView(BrandItem item, View convertView) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = LayoutInflater.from(RegisterCarBrandActivity.this).inflate(R.layout.layout_car_brank_header_item, null);
				holder = new ViewHolder();
				holder.mTvTitle = (TextView) convertView.findViewById(R.id.car_brand_header);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.mBrandItem = item;
			holder.mTvTitle.setVisibility(View.VISIBLE);
			holder.mTvTitle.setText(item.mHeader);
			
			return convertView;
		}
		
		private View getItemView(BrandItem item, View convertView) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = LayoutInflater.from(RegisterCarBrandActivity.this).inflate(R.layout.layout_car_brand_item, null);
				holder = new ViewHolder();
				holder.mTvTitle = (TextView) convertView.findViewById(R.id.car_brand_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.mBrandItem = item;
			holder.mTvTitle.setVisibility(View.VISIBLE);
			holder.mTvTitle.setText(item.mCarBrand.mBrand);
			
			return convertView;
		}
		
		@Override
		public int getViewTypeCount() {
			return TYPE_COUNT;
		}
		
		@Override
		public int getItemViewType(int position) {
			BrandItem item = getItem(position);
			return item.mViewType;
		}

		@Override	//OnScrollListener
		public void onScroll(AbsListView listView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			mListView.configureHeaderView(firstVisibleItem);
		}

		@Override	//OnScrollListener
		public void onScrollStateChanged(AbsListView listView, int state) {
		}

		@Override
		public int getPinnedHeaderState(int position) {
			//TODO
			return PINNED_HEADER_GONE;
		}

		@Override
		public void configurePinnedHeader(View header, int position, int alpha) {
			//TODO
		}

		@Override	//SelectionIndexer
		public int getPositionForSection(int selection) {
			int position = 0;
			BrandItem item = null;
			do {
				item = getItem(position);
				position = position + item.mCount + 1;
			} while(item.mIndex != selection);
			return position;
		}

		@Override	//SelectionIndexer
		public int getSectionForPosition(int position) {
			BrandItem item = getItem(position);
			return item.mIndex;
		}

		@Override	//SelectionIndexer
		public Object[] getSections() {
			return mSelections.toArray();
		}
	}
	
	
}
